package com.example.demo.util.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author xkx
 * @version 1.0.0
 * @description
 * @since 2019/3/8 10:55
 */
public class HttpURLConnectionTest {

  private static final Logger log = LoggerFactory.getLogger(HttpURLConnectionTest.class);

  public static void main(String[] args) {
    try {
      // 1. 获取访问地址URL
      URL url = new URL("http://ralphfront.ezhiyang.sh/signIn");
      // 2. 创建HttpURLConnection对象
      HttpURLConnection connection = (HttpURLConnection) url
          .openConnection();
      /* 3. 设置请求参数等 */
      // 请求方式
      connection.setRequestMethod("POST");
      // 超时时间
      connection.setConnectTimeout(3000);
      // 设置是否输出
      connection.setDoOutput(true);
      // 设置是否读入
      connection.setDoInput(true);
      // 设置是否使用缓存
      connection.setUseCaches(false);
      // 设置此 HttpURLConnection 实例是否应该自动执行 HTTP 重定向
      connection.setInstanceFollowRedirects(true);
      // 设置使用标准编码格式编码参数的名-值对
      connection.setRequestProperty("Content-Type",
          "application/json");
      // 连接
      connection.connect();
      /* 4. 处理输入输出 */
      // 写入参数到请求中
      String params = "{\"loginName\":\"17625582353\",\"password\":\"VVAfjp35\",\"serviceUrl\":\"http://ralphfront.ezhiyang.sh/#/authTransit?redirectUrl=/MySalary\"}";
      OutputStream out = connection.getOutputStream();
      out.write(params.getBytes());
      out.flush();
      out.close();
      // 从连接中读取响应信息
      String msg = "";
      int code = connection.getResponseCode();
      if (code == 200) {
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(connection.getInputStream()));
        String line;

        while ((line = reader.readLine()) != null) {
          msg += line + "\n";
        }
        reader.close();
      }
      // 5. 断开连接
      connection.disconnect();

      // 处理结果
      System.out.println(msg);
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
